package mcu.market.swing;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.BoxLayout;

import java.awt.BorderLayout;

import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.FlowLayout;
import java.awt.Component;
import java.awt.Panel;

import javax.swing.JButton;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.JScrollPane;

import mcu.market.ibatis.IBatisFactory;
import mcu.market.ibatis.bean.BeanCompany;
import mcu.market.ibatis.bean.BeanDailyPrice;
import mcu.market.util.DateUtil;

import org.apache.ibatis.session.SqlSession;
import org.dom4j.Node;
import org.postgresql.util.PSQLException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class RSI_panel extends JPanel {
	private JTable table;
	private JTextField rsi_t;
	private JTextField sid_t;
	private String[] titleField = new String[]{"日期","收盤價","RSI"};
	private String [][] tmpTableData={{"","",""}};
	private JTextField RSI_b;
	private JTextField RSI_s;
	/**
	 * Create the panel.
	 */
	public RSI_panel() {
		DefaultTableModel tmodel = new DefaultTableModel();
		tmodel.setColumnIdentifiers(titleField);
		setLayout(new BorderLayout(0, 0));
		
		table = new JTable(tmodel);
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		
		table.setFillsViewportHeight(true);
		table.validate();
		
		
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		
		JLabel sid = new JLabel("各股代號");
		
		sid_t = new JTextField();
		sid_t.setColumns(10);
		
		JLabel day = new JLabel("RSI 天數");
		
		rsi_t = new JTextField();
		rsi_t.setColumns(10);
		
		JButton search = new JButton("確認");
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int rsi=0;
				try{
					rsi=Integer.parseInt(rsi_t.getText());
					if(!"".equals(RSI_b.getText()))Integer.parseInt(RSI_b.getText());
					if(!"".equals(RSI_s.getText()))Integer.parseInt(RSI_s.getText());
					if(!"".equals(sid_t.getText())&&!"".equals(rsi_t.getText())){
						SqlSession sqlSession=IBatisFactory.getInstance().getSqlSession();
						Map map =new HashMap();
						map.put("sid", sid_t.getText());
						map.put("rsi", rsi);
						List<BeanDailyPrice> list=sqlSession.selectList("daily_price.selectPriceRSI",map);	
						setData(list);					
						sqlSession.commit();  
						sqlSession.close();
					}else{
						JOptionPane.showMessageDialog(null, "請完整查詢條件");
					}
				}catch (NumberFormatException e){
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "值錯誤!!\n"+e.getMessage());
				}catch (Exception e){
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "查詢錯誤!!\n"+e.getMessage());
				}
				
			}
		});
		
		JLabel lblRsi = new JLabel(">RSI>");
		
		RSI_b = new JTextField();
		RSI_b.setColumns(10);
		
		RSI_s = new JTextField();
		RSI_s.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("");
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(59)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(sid)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(sid_t, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(lblNewLabel))
					.addGap(50)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(day)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(rsi_t, 0, 0, Short.MAX_VALUE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(RSI_b, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblRsi)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(RSI_s, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
					.addComponent(search)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(sid)
						.addComponent(sid_t, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(day)
						.addComponent(rsi_t, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(RSI_b, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblRsi)
						.addComponent(RSI_s, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel)
						.addComponent(search))
					.addGap(11))
		);
		panel.setLayout(gl_panel);

	}
	public void setData(List<BeanDailyPrice> list){
		int i=0;
		DateUtil du=new DateUtil();
		DefaultTableModel tmodel=new DefaultTableModel();
		titleField[2]="RSI("+rsi_t.getText()+")";
		tmodel.setColumnIdentifiers(titleField);
		for(BeanDailyPrice bean:list){
			tmodel.addRow(new Object[]{du.DateToString(bean.getDay(), "YYYY-MM-dd"),String.valueOf(bean.getClose()),String.valueOf(bean.getRsi())});
		}
		table.setDefaultRenderer(Object.class, new MyTableCellRender(RSI_b.getText(), RSI_s.getText()));
		table.setModel(tmodel);
		
	}
}
class MyTableCellRender extends DefaultTableCellRenderer {  
	int b=-1;
	int s=-1;
    public MyTableCellRender(String rsi_b,String rsi_s){
    	if(!"".equals(rsi_b))b=Integer.parseInt(rsi_b);
    	if(!"".equals(rsi_s))s=Integer.parseInt(rsi_s);
        setOpaque(true);  
    }   
    public Component getTableCellRendererComponent(JTable table, Object value,   
            boolean isSelected, boolean hasFocus, int row, int column)   
    {   
        if(Double.parseDouble((String)table.getValueAt(row, 2)) >b&&b!=-1)  
        {  
            setForeground(Color.black);          
            setBackground(Color.red);              
        }else if(Double.parseDouble((String)table.getValueAt(row, 2)) <s&&s!=-1){
        	setForeground(Color.black);          
            setBackground(Color.green);
        }else{      
            setBackground(Color.white);      
            setForeground(Color.black);      
        }   
        setText(value !=null ? value.toString() : "");  
        return this;  
    }  
}  